﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using BOSS.Service.Security;
using BOSS.Domain.Security;

namespace BOSS.Service.Implements.Security
{
    /// <summary>
    /// 表单验证的辅助类
    /// </summary>
    internal sealed class FormsAuthenticationHelper
    {
#if DEBUG
        private const string Domain = "eportal.taoke.cn";
#else
        private const string Domain = "eportal.taoke.com";
#endif

        /// <summary>
        /// 根据登录信息进行登录
        /// </summary>
        /// <param name="loginInfo"></param>
        internal static void SignIn(LoginInfo loginInfo)
        {
            if (loginInfo == null || loginInfo.Account == null)
            {
                return;
            }

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
              loginInfo.Account.FullName,
              loginInfo.LoginTime,
              loginInfo.ExpireTime,
              false,
              loginInfo.Guid.ToString(),
              FormsAuthentication.FormsCookiePath);
            string encTicket = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
            {
                //Domain = FormsAuthenticationHelper.Domain,
            };
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 注销
        /// </summary>
        internal static void SignOut()
        {
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName)
            {
                //Domain = FormsAuthenticationHelper.Domain,
                Expires = DateTime.MinValue,
            };
            HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 跳转到登录页
        /// </summary>
        internal static void RedirectToLoginPage()
        {
            FormsAuthentication.RedirectToLoginPage();
        }
    }
}
